/*
 * WatchTreeNode.java
 * 
 * last update: 14.01.2010 by Stefan Saru
 * 
 * author:	Lucian Ghita Felix(felix@ulise.cs.pub.ro)
 *
 * Obs: N/A
 */

package ide;

import javax.swing.*;
import javax.swing.tree.*;
import middle.*;
public class WatchTreeNode implements ScopeNode, LeafNode {

	final public static int FILETYPE   = 0;
	final public static int MODULETYPE = 1;
	final public static int MODARRAYTYPE = 9;

	final public static int WIRETYPE   = 2;
	final public static int REGTYPE    = 3;

	final public static int PORTTYPE   = 4;
	final public static int INPUTTYPE  = 5;
	final public static int OUTPUTTYPE = 6;
	final public static int INOUTTYPE  = 7;
	final public static int PROJECTTYPE  = 8;
	//todo: alte icoane. de ce DRACU' nu folosshti middle.Constants ?????

	static ImageIcon []  icons = new ImageIcon[10];

	static {
		for(int i = 0; i < 10 ; i++){
			icons[i] = new ImageIcon(vide.baseDir + "images/tree" + i + ".gif");
		}
	} 

	public int type;
	public String name;
	public String caption;
	public boolean isLeaf;
	public Object data; //(used unly when this is a leaf)
	private DefaultMutableTreeNode holder;
	public JTree tree;

	public WatchTreeNode(DefaultMutableTreeNode holder, int type, String name){
		this.holder = holder;
		this.name = name;
		this.type = type;
		caption = "..";
		isLeaf = false;
	}

	public WatchTreeNode(DefaultMutableTreeNode holder, int type, String name, Object data){
		this.holder = holder;
		this.name = name;
		this.type = type;
		this.data = data;
		caption = "not initialized.(PM)";
		isLeaf = true;
	}

	public String toString(){
		if (isLeaf) return name + "   :   " + caption;
		else return name;
	}

	/**returns the fully qualified name*/
	public String getFQN(){
		if (type != PROJECTTYPE) 
			return  ((WatchTreeNode)((DefaultMutableTreeNode)((DefaultMutableTreeNode)holder).getParent()).getUserObject()).getFQN() + "." + name; 
		else 
			return new String("");
	}

	public boolean equals(Object o) {
		return o.toString().equals(name);
	}

	public ImageIcon getIcon() {
		return icons[type];
	}

	public ScopeNode addScope(int icon, String name) {
		DefaultMutableTreeNode newNode = new DefaultMutableTreeNode();
		WatchTreeNode newNodeData= new WatchTreeNode(newNode, icon, name);
		newNode.setUserObject(newNodeData);
		newNodeData.tree = tree;
		holder.add(newNode);
		return newNodeData;
	}

	public LeafNode addLeaf(int icon, String name, Object data) {
		DefaultMutableTreeNode newNode = new DefaultMutableTreeNode();
		WatchTreeNode newNodeData= new WatchTreeNode(newNode, icon, name, data);
		newNode.setUserObject(newNodeData);
		newNode.setAllowsChildren(false);
		newNodeData.tree = tree;
		holder.add(newNode);
		return newNodeData; 
	}

	/**called when the value has changed.
	it's illegal to call when not leaf.*/
	public void update() {
		if (!isLeaf) {
			System.out.println("Trying to Notify() a Scope.That's a stupid thing to do, or a bug.");
			return;
		}
		if(data == null) {
			System.out.println("I have no data for watchTreeNode.repaint()! You have a buug! Ta na nana na !");
			return;
		}
		caption = data.toString();
		tree.revalidate();
		tree.repaint();
	}
} 
